/**
 * JavaScript
 *
 * Copyright (C)
 * This program is free software. You can redistribute it and/or modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation. Either version 2 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY. Without even the implied warranty
 * of ERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program. Ff not, write to the Free Software
 * Fundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
 *
 * @author Bastian Schröder <bs@chemanet.de>
 * @version 2008-04-10
 * @package railadmin
 */


/**
 * Update data store for mysql_db.
 *
 */
function mysql_databases () {
	var store = new dojo.data.ItemFileReadStore({url:"call.php?func=mysql_databases"});
	dijit.byId('mysql_db').store = store;
}

/**
 * Load module
 *
 */
function load_module ( name ) {
	ajax_call( 'load_module&name='+name, {load: function( ajax_response ) {
		if( !document.getElementById('module_'+name+'_dialog') ){
			// create HTML code for iframe
			var iframe = document.createElement('div');
			iframe.innerHTML = '<iframe src="modules/'+name+'/index.php" width="'+ajax_response.width+'" height="'+ajax_response.height+'" class="module_iframe" />';

			// create Dialog
			new dijit.Dialog({id: 'module_'+name+'_dialog', title: ajax_response.title, refreshOnShow: 'true' }, iframe);
		}

		dijit.byId( 'module_'+name+'_dialog' ).show();

	}} );
}

/**
 * Refresh view.
 *
 */
function refresh () {
	mysql_databases();
	rail_log_init();
	rail_tree_init();
	rail_patche();

	// activate modules
	var buttons = document.getElementsByTagName("button");
	for( i=0; i < buttons.length; i++ ){
		if( buttons[i].id.substr(0, 7) != 'module_' ) continue;
		dijit.byId( buttons[i].id ).setDisabled( false );
	}

	// activate download
	dijit.byId('backup_download').setDisabled(false);
	document.getElementById('backup_upload').disabled = false;
}


/* --------------------------------------- rail --------------------------------------- */
/**
 * Try to connect to a RAIL.
 *
 */
function rail_connect () {
	ajax_call_post( 'mysql_form', 'rail_connect', {load: function( ajax_response ) {
		// on error response
		if( ajax_response.type == 'ajax_error_response' ){
			if( !ajax_response.connect_mysql ) dijit.byId('mysql_db').setDisabled(true);
			alert("Die Verbindung konnte nicht hergestellt werden:\n" + ajax_response.exceptions[0] );
			return
		}

		mysql_databases();
		dijit.byId('mysql_db').setDisabled(false);
		dijit.byId('mysql_newdb').setDisabled(false);
		dijit.byId('addmysql_form_submit').setDisabled(false);

		// if connected to rail
		if( ajax_response.connect_rail ){
			refresh();
			document.getElementById('status').innerHTML = dijit.byId('mysql_user').getValue() + '@' + dijit.byId('mysql_host').getValue() + ':' + dijit.byId('mysql_db').getValue();
			dijit.byId('mainTabContainer').selectChild( dijit.byId('log') );
		}
		else document.getElementById('status').innerHTML = dijit.byId('mysql_user').getValue() + '@' + dijit.byId('mysql_host').getValue();
	}} );
}


/**
 * Create a new RAIL and MySQL databases.
 *
 */
function rail_create () {
	ajax_call_post( 'addmysql_form', 'rail_create', {load: function( ajax_response ) {
		// on error response
		if( ajax_response.type == 'ajax_error_response' ){
			alert( ajax_response.exceptions[0] );
			return;
		}

		refresh();
		var new_db = dijit.byId('mysql_newdb').getValue();
		dijit.byId('mysql_db').setValue( new_db );
		dijit.byId('mysql_newdb').setValue('');
		document.getElementById('status').innerHTML = dijit.byId('mysql_user').getValue() + '@' + dijit.byId('mysql_host').getValue() + ':' + new_db;
		dijit.byId('mainTabContainer').selectChild( dijit.byId('log') );
	}} );
}


/**
 * Add a message to rail_log.
 *
 * @param string message
 * @param string type
 */
function rail_log_add ( message, type ) {
	html_code = '<p class="' + ( type.length > 0 ? 'error ' + type : 'error' ) + '">' + message + '</p>';
	dijit.byId('log').setContent( dijit.byId('log').domNode.innerHTML + html_code );
}


/**
 * Init the rail_log.
 *
 */
function rail_log_init () {
	ajax_call( 'rail_log', {load: function( ajax_response ) {
		dijit.byId('log').setContent( '' );
		for( i=0; i < ajax_response.messages.length; i++ )
			rail_log_add( ajax_response.messages[i].text, ajax_response.messages[i].type );
	}} );
}


/**
 * Try to login a rail_user.
 *
 */
function rail_login () {
	ajax_call_post( 'login_form', 'rail_login', {load: function( ajax_response ) {
		// on error response
		if( ajax_response.type == 'ajax_error_response' ){
			alert("Die Anmeldung ist gescheitert:\n" + ajax_response.exceptions[0] );
			return;
		}

		rail_tree_init();
		dijit.byId('mainTabContainer').selectChild( dijit.byId('log') );
		rail_log_add( 'Der Benutzer \'' + ajax_response.logedin + '\' wurde am RAIL angmeldet.', 'info' );
		dijit.byId('login').hide();
	}} );
}


/**
 * Disable/Enable available patche.
 *
 */
function rail_patche () {
	ajax_call( 'rail_log', {load: function( ajax_response ) {
		var buttons = document.getElementsByTagName("button");
		for( i=0; i < buttons.length; i++ ){
			if( buttons[i].id.substr(0, 6) != 'patch_' )
				continue;

			var require = document.getElementById( buttons[i].id + '_require' ).value;
			dijit.byId( buttons[i].id ).setDisabled( require != ajax_response.rail_version );
		}
	}} );
}


/**
 * Install a patch to connected RAIL.
 *
 * @param string file
 */
function rail_patche_install ( file ) {
	ajax_call( 'rail_patch_install&file='+file, {load: function( ajax_response ) {
		refresh();
	}} );
}


/**
 * Init rail_tree.
 *
 */
function rail_tree_init () {
	dijit.byId('railtree').setContent( '<div dojoType="dojo.data.ItemFileReadStore" url="call.php?func=rail_tree" jsid="railtree" /><div dojoType="dijit.Tree" store="railtree" labelAttr="name" label="root (#1)"></div></div>' );
}